/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DocUpload;

import java.io.*;
import org.apache.commons.fileupload.FileItemStream;

/**
 *
 * @author Mukesh
 */
public class FileUpload {
    

    public static String writeFile(FileItemStream file, String filename) throws FileNotFoundException, IOException {
String fullFileName="";
try{
        String path = "/usr/RITDocuments";
        
        File fi = new File(path);
        if (!fi.exists()) {
            fi.mkdir();
        }
        String ext = file.getName().substring(file.getName().lastIndexOf('.'));
        
         fullFileName = filename + ext;
        File savefile = new File(fi.getAbsolutePath() + File.separator + fullFileName);
        
        InputStream i = file.openStream();
        FileOutputStream fout = new FileOutputStream(savefile);
        int x = 0;
        byte b[] = new byte[1024];
        while ((x = i.read(b)) != -1) {
            fout.write(b, 0, x);
        }
        fout.flush();
        fout.close();
        
}catch(Exception e){
    System.out.println(e);
}return fullFileName;
    }
}
